package cn.com.xuhx.testMapper;

import cn.com.xuhx.mapper.DynamicMapper;
import cn.com.xuhx.pojo.User;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * 动态SQL
 */
public class DynamicSQLTest {

    private SqlSession sqlSession = null;

    @Before
    public void getSQLSession() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //设置需要手动提交事务
        //sqlSession = build.openSession();
        //设置自动提交事务
        sqlSession = build.openSession(true);
    }

    @After
    public void closeSQLSession(){
        sqlSession.clearCache();
        sqlSession.close();
    }

    /**
     * if标签
     */
    @Test
    public void ifTest(){
        DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
        List<User> users = mapper.queryUser(null, null, null, 20, 23,
                null,null,null,null);
        System.out.println(users);
    }

    /**
     * 分页
     */
    @Test
    public void pageTest(){
        DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
        Page<Object> page = PageHelper.startPage(1, 2);
        List<User> users = mapper.queryUser(null, null, null, null, null,
                null,null,null,null);
        System.out.println(users);
        System.out.println(page.getTotal());
    }

    /**
     * choose标签
     */
    @Test
    public void chooseTest(){
        DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
        List<User> users = mapper.queryUser2(null, null, null, 20, 23,
                null,null,null,null);
        System.out.println(users);
    }

    /**
     * foreach标签
     */
    @Test
    public void deleteUsers(){
        DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
        int count = mapper.deleteUserbyIDs(new Integer[]{2, 6});
        System.out.println(count);
    }

    @Test
    public void deleteUsers2(){
        DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
        int count = mapper.deleteUserbyIDs2(new Integer[]{2, 6});
        System.out.println(count);
    }

    @Test
    public void batchAddUser() throws ParseException {
        DynamicMapper mapper = sqlSession.getMapper(DynamicMapper.class);
        User user1 = new User("张三", 23, "南通", "18115180222", "320682199101011111", DateUtil.parse("1991-01-01","yyyy-MM-dd"));
        User user2 = new User("李四", 24, "启动", "18115180212", "320682199101011131", DateUtil.parse("1992-01-01","yyyy-MM-dd"));
        User user3 = new User("王五", 25, "上海", "18115180252", "320682199101011141", DateUtil.parse("1993-01-01","yyyy-MM-dd"));
        mapper.batchAddUsers(Arrays.asList(user1,user2,user3));
    }
}
